package com.medical_houduan.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical_houduan.commons.Result;
import com.medical_houduan.dto.SaleDto;
import com.medical_houduan.mapper.SaleMapper;
import com.medical_houduan.pojo.Sale;
import com.medical_houduan.service.SaleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SaleServiceImpl implements SaleService {
    @Autowired
    SaleMapper saleMapper;

    /**
     * 获取所有的销售地点，如果存在不为空的关键字，则进行关键字查询
     * @param currentPage
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public Result getSales(Integer currentPage, Integer pageSize, String keyword) {
        Page<Sale> page = new Page<>(currentPage,pageSize);
        if(keyword==null||"".equals(keyword)) {
            //获取所有销售地点
            QueryWrapper<Sale> wrapper = new QueryWrapper<>();
            Page<Sale> pg = saleMapper.selectPage(page,wrapper);
            if (pg.getRecords().isEmpty()){
                return new Result(201,"没有数据了");
            }
            return new Result(200,"已获取√",pg);
        }

        //关键字不为空,获取包含关键字的销售地点
        QueryWrapper<Sale> wrapper1 = new QueryWrapper<>();
        wrapper1.like("sale_name",keyword);
        Page<Sale> pg_key = saleMapper.selectPage(page,wrapper1);
        if (pg_key.getRecords().isEmpty()){
            return new Result(202,"没有查询的数据了");
        }
        return new Result(200,"已获取√",pg_key);
    }

    /**
     * 通过saleId查询销售地点
     * @param saleId
     * @return
     */
    @Override
    public Result getSaleById(Long saleId) {
        if(saleId==null){
            return new Result(201,"指定的销售地点数据为空");
        }
        QueryWrapper<Sale> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sale_id",saleId);
        Sale sale = saleMapper.selectOne(queryWrapper);
        if (sale==null){
            return new Result(202,"该销售地点不存在");
        }
        return new Result(200,"查询成功√",sale);
    }

    /**
     * 修改销售地点信息
     * @param saleDto
     * @return
     */
    @Override
    public Result updateSale(SaleDto saleDto) {
        if (saleDto==null){
            return new Result(201,"修改的数据不能为空");
        }
        if(saleDto.getSaleId()==null){
            return new Result(201,"Id不能为空");
        }
        if ("".equals(saleDto.getSaleName())||saleDto.getSaleName()==null){
            return new Result(202,"修改的销售地点名称不能为空");
        }
        if ("".equals(saleDto.getSalePhone())||saleDto.getSalePhone()==null){
            return new Result(203,"修改的销售地点联系号码不能为空");
        }
        //查询销售地点是否存在数据库
        QueryWrapper<Sale> wrapper = new QueryWrapper<>();
        wrapper.eq("sale_id",saleDto.getSaleId());
        Sale sale=saleMapper.selectOne(wrapper);
        if(sale==null){
            return new Result(205,"指定修改的销售地点不存在数据库");
        }
        //属性复制
        Sale sale1 = new Sale();
        BeanUtils.copyProperties(saleDto,sale1);
        sale1.setUpdateTime(new Date());

        //更新
        int row = saleMapper.updateById(sale1);
        if(row!=1){
            return new Result(204,"修改失败,请稍后重试");
        }
        return new Result(200,"修改成功√",sale1);

    }

    /**
     * 添加销售地点
     * @param saleDto
     * @return
     */
    @Override
    public Result addSale(SaleDto saleDto) {
        if (saleDto==null){
            return new Result(201,"添加的数据不能为空");
        }
        if ("".equals(saleDto.getSaleName())||saleDto.getSaleName()==null){
            return new Result(202,"添加的销售地点名称不能为空");
        }
        if ("".equals(saleDto.getSalePhone())||saleDto.getSalePhone()==null){
            return new Result(203,"添加的销售地点的联系号码不能为空");
        }
        //属性复制，将SaleDTO对象的属性值复制给Sale对象
        Sale sale = new Sale();
        BeanUtils.copyProperties(saleDto,sale);
        sale.setCreateTime(new Date());
        sale.setUpdateTime(new Date());
        int row = saleMapper.insert(sale);
        if(row!=1){
            return new Result(204,"添加失败,请稍后重试");
        }
        return new Result(200,"添加成功√");
    }

    /**
     * 通过saleId删除销售地点数据
     * @param saleId
     * @return
     */
    @Override
    public Result deleteSale(Long saleId) {
        if(saleId==null){
            return new Result(202,"没有指定的删除数据");
        }
        int row=saleMapper.deleteById(saleId);
        if(row!=1){
            return new Result(201,"删除失败，稍后重试");
        }
        return new Result(200,"删除成功√");
    }

}
